<?php

error_reporting(E_ALL ^ E_NOTICE);

require_once ('../api/classes/phpexcel/Classes/PHPExcel/IOFactory.php');

include_once("../api/classes/gbif.custom-eml.php");
include_once("../api/classes/gbif.occurrence.php");
include_once("../api/classes/gbif.general.php");
// include_once('../config.php');

$template = trim($_REQUEST['template']);
$template = ($template == '') ? 'checklist-1.1.xlsx' : $template;
$folder = './files/';

if(!file_exists($folder)) {
	@mkdir($folder,0775);
}

$url = 'http://www.silverbiology.com/clients/gbif/spreadsheet-processor/unittests/files/checklist.zip';

echo '<br> File Name : ' . $template;

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($template);
$sheet = $objPHPExcel->getSheet(0);

$version = $sheet->getCell('B3')->getValue();
echo "<br> Version : " . $version;

switch($version) {
case 'Checklist-1.1':
// echo '<br> In switch';
	$objPHPExcel->setActiveSheetIndex(2);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
	$objWriter->setDelimiter(',');
	$objWriter->setEnclosure('');
	$objWriter->setLineEnding("\r\n");
	$objWriter->setSheetIndex(2);
	$objWriter->save($folder . 'checklist.csv');
	
	# Make EML
	$eml = new CustomEml();
	$sheet = $objPHPExcel->getSheet(1);
	$eml->createEmlFromWorksheet( $sheet );
	$eml->generateXml();
	$xml = $eml->getRawXml();
	file_put_contents($folder . 'eml.xml', $xml );
	
	# Make metaxml

// 	$sh = new Occurrence( $template );
// 	$sh->getSheet(1);
// 	file_put_contents($folder . 'meta.xml', $sh->generateOccurrenceDataMeta() );

// 	$sheet->zipArchive($folder . 'checklist.zip', $folder, array('eml.xml', 'meta.xml', 'checklist.csv'));

	$general = new General();
	$general->zipArchive($folder . 'checklist.zip', $folder, array('eml.xml', 'checklist.csv'));

echo '<br><a href="' . $url . '"> Zip File Path : </a>';

	break;
} # switch
?>